import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import EditorView from '../views/EditorView.vue'
import { WORKDIR_UNDEFINED, getStore } from "@/api/store"
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: EditorView
  },
  {
    path: '/guide',
    name: 'guide',
    // route level code-splitting
    // this generates a separate chunk (guide.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "guide" */ '../views/GuideView.vue')
  },
  {
    path: '/chart',
    name: 'chart',
    // route level code-splitting
    // this generates a separate chunk (guide.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "chart" */ '../views/ChartView.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})


router.beforeEach((to, from, next) => {
  // 1. 路由守卫
  getStore().then((store) => {
    const wd = store.state.workdir
    if (wd == WORKDIR_UNDEFINED && to.name == 'home') {
      return router.replace({
        name: 'guide'
      })
    }else{
      next()
    }
  })
})

export default router

